var util = require('../../utils/util.js');
var api = require('../../config/api.js');
var user = require('../../services/user.js');
var app = getApp();

Page({
    data: {
        tabs: [],
        activeTab: 0,
        defineNum: 4,
        content: [],
        startDate: "",
        endDate: "",
        pickEndDate: "",
        showPicker: false,
        timeParams: [],
        pageSize:5,
        pageNo:1,
        orderList:[],
        hasMore:true,
        status:1,
        layShow: false
    },
  
    onLoad() {
        // const titles = ['待使用', '已使用', '售后']
        // const tabs = titles.map(item => ({title: item}))
        // this.setData({tabs})
        const titles = ['待使用', '已使用', '售后']
        const tabs = titles.map(item => ({title: item}))
        let content = titles.map(item => ({title: item}))
        let userInfo = wx.getStorageSync("userInfo")
        //console.log(userInfo)
        this.setData({
            merchantId:userInfo.merchantId
        })
        this.setData({tabs, content})
        this.getData(this.data.status)
    },
    onReachBottom(){
        this.loadMore(this.data.status)
    },
    getData(status){
        let that = this;
        that.setData({
            pageNo: 1,
            hasMore:true
        });
        wx.showLoading({
            title: '加载中...',
          })
        util.request(api.surroundings.getMerchantOrderListByMerchantId,{
            merchantId:that.data.merchantId,
            pageNo:that.data.pageNo,
            pageSize:that.data.pageSize,
            status:status
        },'get').then(res=>{
            that.setData({
                orderList: res.result
            });
            if (res.result.length < this.data.pageSize) {
                that.setData({
                    hasMore: false,
                })
            }
            wx.hideLoading()
        }).catch(err=>{
            console.log(err)
            wx.hideLoading()
        })
    },
    loadMore: function(status) {
        var that = this;
        if (!this.data.hasMore) return
        wx.showLoading({
            title: '加载中...',
        })
        util.request(api.surroundings.getMerchantOrderListByMerchantId,{
            merchantId:that.data.merchantId,
            pageNo:++that.data.pageNo,
            pageSize:that.data.pageSize,
            status:status
        },'get').then(res=>{
            if (res.code == 200) {
                that.setData({
                    orderList: that.data.orderList.concat(res.result)
                });
                if (res.result.length < that.data.pageSize) {
                    that.setData({
                        hasMore: false,
                    })
                }
                wx.hideLoading()
            }
        });
    },
    onChange(e) {
        const index = e.detail.index
        this.setData({
            activeTab: index
        })
    },
    switchTab(e){
        const id = e.currentTarget.dataset.id;
        this.setData({
            activeTab: id
        })
        if(this.data.activeTab === 0){
            this.setData({
                status:1
            })
        } else if(this.data.activeTab === 1){
            this.setData({
                status:2
            })
        } else if(this.data.activeTab === 2){
            this.setData({
                status:9
            })
        }
        this.getData(this.data.status)
    },
  
})
  